﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using padnug.Domain;
using PadnugFramework;
using PadnugRepositoryInterfaces;

namespace PadnugRepository
{
    public class PadnugEventsRepository : IPadnugEventsRepository
    {
        private padnugEntities db; 
        public PadnugEventsRepository()
        {
            db = new padnugEntities();
        }
        #region IPadnugEventsRepository Members

        public Event GetEvent(int Id)
        {
            Event padnugEvent;
            try
            {
                var query = from m in db.Meetings.Where(m => m.ID == Id)
                            join ms in db.MeetingLocationLookups
                                 on m.ID equals ms.MeetingId
                            join l in db.LocationLatLongs
                                on ms.LocationLatLongId equals l.LatLongId
                            select new Event
                            {
                                ID = m.ID,
                                Topic = m.Topic,
                                SpeakerName = m.SpeakerName,
                                HostDescription = m.HostDescription,
                                TopicDescription = m.TopicDescription,
                                MeetingStart = m.MeetingStart,
                                MeetingEnd = m.MeetingEnd,
                                SpeakerPhotoUrl = string.IsNullOrWhiteSpace(m.SpeakerPhotoUrl) ? "~/content/images/blank.png" : m.SpeakerPhotoUrl,
                                SpeakerUrl = m.SpeakerUrl,
                                OrganizationName = m.OrganizationName,
                                Location = l.Location,
                                Address1 = l.Address1,
                                Address2 = l.Address2,
                                City = l.City,
                                State = l.State,
                                Zip = l.Zip,
                                Latitude = l.Latitude,
                                Longitude = l.Longitude
                            };

                padnugEvent = query.FirstOrDefault();
            }
            catch (Exception)
            {
                padnugEvent = new Event();
            }
            return padnugEvent;
        }

        private Event GetEventWithJoin(int meetingId)
        {
            var query = from m in db.Meetings.Where(m => m.ID == meetingId) 
                        join ms in db.MeetingLocationLookups
                             on m.ID equals ms.MeetingId
                        join l in db.LocationLatLongs
                            on ms.LocationLatLongId equals l.LatLongId
                        select new Event
                        {
                            ID = m.ID,
                            Topic = m.Topic,
                            SpeakerName = m.SpeakerName,
                            HostDescription = m.HostDescription,
                            TopicDescription = m.TopicDescription,
                            MeetingStart = m.MeetingStart,
                            MeetingEnd = m.MeetingEnd,
                            SpeakerPhotoUrl = string.IsNullOrWhiteSpace(m.SpeakerPhotoUrl) ? "~/content/images/blank.png" : m.SpeakerPhotoUrl,
                            SpeakerUrl = m.SpeakerUrl,
                            OrganizationName = m.OrganizationName,
                            Location = l.Location,
                            Address1 = l.Address1,
                            Address2 = l.Address2,
                            City = l.City,
                            State = l.State,
                            Zip = l.Zip,
                            Latitude = l.Latitude,
                            Longitude = l.Longitude
                        };
            return query.FirstOrDefault();
        }


        public IList<Event> GetEvents(int? skip, int? take)
        {
            IList<Event> events = new List<Event>();
            try
            {
                //var dbEvents = db.Meetings.OrderByDescending(m => m.MeetingDate).Skip(skip.Value).Take(take.Value);
                //foreach (var dbEvent in dbEvents)
                //{
                //    Event e = new Event
                //                  {
                //                      Host = dbEvent.Host,
                //                      HostSummary = dbEvent.HostSummary,
                //                      ID = dbEvent.ID,
                //                      Latitude = dbEvent.Latitude,
                //                      Location = dbEvent.Location,
                //                      Longitude = dbEvent.Longitude,
                //                      MeetingDate = dbEvent.MeetingDate,
                //                      MeetingEnd = dbEvent.MeetingEnd,
                //                      MeetingStart = dbEvent.MeetingStart,
                //                      MeetingTime = dbEvent.MeetingTime,
                //                      Minutes = dbEvent.Minutes,
                //                      Topic = dbEvent.Topic,
                //                      TopicSummary = dbEvent.TopicSummary
                //                  };
                //    events.Add(e);
                //}
                skip = skip.HasValue ? skip.Value : 0;
                take = take.HasValue ? take.Value : 10;
                var query = from m in db.Meetings
                            join ms in db.MeetingLocationLookups
                                 on m.ID equals ms.MeetingId
                            join l in db.LocationLatLongs
                                on ms.LocationLatLongId equals l.LatLongId
                            select new Event
                            {
                                ID = m.ID,
                                Topic = m.Topic,
                                SpeakerName = m.SpeakerName,
                                HostDescription = m.HostDescription,
                                TopicDescription = m.TopicDescription,
                                MeetingStart = m.MeetingStart,
                                MeetingEnd = m.MeetingEnd,
                                SpeakerPhotoUrl = string.IsNullOrWhiteSpace(m.SpeakerPhotoUrl) ? "~/content/images/blank.png" : m.SpeakerPhotoUrl,
                                SpeakerUrl = m.SpeakerUrl,
                                OrganizationName = m.OrganizationName,
                                Location = l.Location,
                                Address1 = l.Address1,
                                Address2 = l.Address2,
                                City = l.City,
                                State = l.State,
                                Zip = l.Zip,
                                Latitude = l.Latitude,
                                Longitude = l.Longitude
                            };
                events = query.OrderBy(q => q.MeetingStart).Skip(skip.Value).Take(take.Value).ToList();
            }
            catch
            {

            }

            return events;
        }

        public bool SaveEvent(Event padnugEvent)
        {
            bool? result = null;
            var dbEvent = new Meeting
                              {
                                  Host = padnugEvent.Host,
                                  HostSummary = padnugEvent.HostSummary,
                                  ID = -1,
                                  Latitude = padnugEvent.Latitude,
                                  Location = padnugEvent.Location,
                                  Longitude = padnugEvent.Longitude,
                                  MeetingEnd = padnugEvent.MeetingEnd,
                                  MeetingStart = padnugEvent.MeetingStart,
                                  MeetingTime = padnugEvent.MeetingTime,
                                  Minutes = padnugEvent.Minutes,
                                  Topic = padnugEvent.Topic,
                                  TopicSummary = padnugEvent.TopicSummary
                              };
            try
            {
                db.AddToMeetings(dbEvent);
                db.SaveChanges();
                result = true;
            }
            catch (Exception)
            {
                result = false;
            }
            return result.Value;
        }

        #endregion

        #region IPadnugEventsRepository Members


        public bool UpdateEvent(Event padnugEvent)
        {
            bool? result = null;
            var dbEvent = db.Meetings.First(m => m.ID == padnugEvent.ID);
            dbEvent.Host = padnugEvent.Host;
            dbEvent.HostSummary = padnugEvent.HostSummary;
            dbEvent.Latitude = padnugEvent.Latitude;
            dbEvent.Location = padnugEvent.Location;
            dbEvent.Longitude = padnugEvent.Longitude;
            dbEvent.MeetingDate = padnugEvent.MeetingDate;
            dbEvent.MeetingEnd = padnugEvent.MeetingEnd;
            dbEvent.MeetingStart = padnugEvent.MeetingStart;
            dbEvent.MeetingTime = padnugEvent.MeetingTime;
            dbEvent.Minutes = padnugEvent.Minutes;
            dbEvent.Topic = padnugEvent.Topic;
            dbEvent.TopicSummary = padnugEvent.TopicSummary;
            dbEvent.SpeakerName = padnugEvent.SpeakerName;
            dbEvent.SpeakerPhotoUrl = padnugEvent.SpeakerPhotoUrl;
            dbEvent.SpeakerUrl = padnugEvent.SpeakerUrl;
            dbEvent.TopicDescription = padnugEvent.TopicDescription;
            dbEvent.HostDescription = padnugEvent.HostDescription;
            dbEvent.OrganizationName = padnugEvent.OrganizationName;
            try
            {
                db.SaveChanges();
                result = true;
            }
            catch
            {
                result = false;
            }
            return result.Value;    
        }

        public bool DeleteEvent(int eventId)
        {
            bool? result = null;

            var dbEvent = db.Meetings.First(m => m.ID == eventId);

            try
            {
                db.DeleteObject(dbEvent);
                db.SaveChanges();
                result = true;
            }
            catch
            {
                result = false;
            }

            return result.Value;
        }

        public IList<Event> GetEventsForADate(DateTime eventDate)
        {
            IList<Event> events = new List<Event>();
            try
            {
                var nextDate = eventDate.AddDays(1);
                var query = from m in db.Meetings.Where(m => m.MeetingDate.Value >= eventDate && m.MeetingDate < nextDate)
                            join ms in db.MeetingLocationLookups
                                 on m.ID equals ms.MeetingId
                            join l in db.LocationLatLongs
                                on ms.LocationLatLongId equals l.LatLongId
                            select new Event
                            {
                                ID = m.ID,
                                Topic = m.Topic,
                                SpeakerName = m.SpeakerName,
                                HostDescription = m.HostDescription,
                                TopicDescription = m.TopicDescription,
                                MeetingStart = m.MeetingStart,
                                MeetingEnd = m.MeetingEnd,
                                SpeakerPhotoUrl = m.SpeakerPhotoUrl,
                                SpeakerUrl = m.SpeakerUrl,
                                OrganizationName = m.OrganizationName,
                                Location = l.Location,
                                Address1 = l.Address1,
                                Address2 = l.Address2,
                                City = l.City,
                                State = l.State,
                                Zip = l.Zip,
                                Latitude = l.Latitude,
                                Longitude = l.Longitude
                            };
                events = query.ToList();
            }
            catch (Exception)
            {

            }
            return events;
        }

        public Event GetFeaturedEvent()
        {
            var today = DateTime.Now;
            var dbEvent = db.Meetings.Where(m => m.MeetingDate.Value >= today).OrderBy(m => m.MeetingDate).Skip(0).Take(1).First();
            Event e = new Event
                    {
                        Host = dbEvent.Host,
                        HostSummary = dbEvent.HostSummary,
                        ID = dbEvent.ID,
                        Latitude = dbEvent.Latitude,
                        Location = dbEvent.Location,
                        Longitude = dbEvent.Longitude,
                        MeetingDate = dbEvent.MeetingDate,
                        MeetingEnd = dbEvent.MeetingEnd,
                        MeetingStart = dbEvent.MeetingStart,
                        MeetingTime = dbEvent.MeetingTime,
                        Minutes = dbEvent.Minutes,
                        Topic = dbEvent.Topic,
                        TopicSummary = dbEvent.TopicSummary,
                        SpeakerPhotoUrl = string.IsNullOrWhiteSpace(dbEvent.SpeakerPhotoUrl) ? "~/content/images/blank.png" : dbEvent.SpeakerPhotoUrl,
                        SpeakerName = dbEvent.SpeakerName,
                        OrganizationName = dbEvent.OrganizationName,
                        HostDescription = dbEvent.HostDescription,
                        TopicDescription = dbEvent.TopicDescription,
                        SpeakerUrl = dbEvent.SpeakerUrl
                    };
            return e;
        }

        public IList<Event> GetUpcomingEvents()
        {
            var today = DateTime.Now;
            IList<Event> events = new List<Event>();

            try
            {
                var dbEvents = db.Meetings.Where(m => m.MeetingDate.Value >= today).OrderBy(m => m.MeetingDate).Skip(1).Take(5);
                foreach (var dbEvent in dbEvents)
                {
                    Event e = new Event
                    {
                        Host = dbEvent.Host,
                        HostSummary = dbEvent.HostSummary,
                        ID = dbEvent.ID,
                        Latitude = dbEvent.Latitude,
                        Location = dbEvent.Location,
                        Longitude = dbEvent.Longitude,
                        MeetingDate = dbEvent.MeetingDate,
                        MeetingEnd = dbEvent.MeetingEnd,
                        MeetingStart = dbEvent.MeetingStart,
                        MeetingTime = dbEvent.MeetingTime,
                        Minutes = dbEvent.Minutes,
                        Topic = dbEvent.Topic,
                        TopicSummary = dbEvent.TopicSummary,
                        SpeakerPhotoUrl = string.IsNullOrWhiteSpace(dbEvent.SpeakerPhotoUrl) ? "~/content/images/blank.png" : dbEvent.SpeakerPhotoUrl,
                        SpeakerName = dbEvent.SpeakerName,
                        OrganizationName = dbEvent.OrganizationName,
                        HostDescription = dbEvent.HostDescription,
                        TopicDescription = dbEvent.TopicDescription,
                        SpeakerUrl = dbEvent.SpeakerUrl
                    };
                    events.Add(e);
                }
            }
            catch (Exception)
            {
                
                throw;
            }
            return events;
        }

        public IList<DateTime> GetEventsForMonth(DateTime firstDayOfMonth)
        {
            var firstDayOfNextMonth = firstDayOfMonth.AddMonths(1);
            var dbDates =
                db.Meetings.Where(e => e.MeetingDate.Value >= firstDayOfMonth && e.MeetingDate.Value < firstDayOfNextMonth).Select(e => e.MeetingDate.Value).ToList();
            return dbDates;
        }

        #endregion
    }
}
